#include<cstdio>//uncle-lu
#include<cstring>
#include<vector>
#include<algorithm>
template<class T>void read(T &x)
{
	x=0;int f=0;char ch=getchar();
	while(ch<'0'||ch>'9') { f|=(ch=='-'); ch=getchar(); }
	while(ch<='9'&&ch>='0') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
	x = f ? -x : x;
	return ;
}

struct node{
	int v, val;
};
int n, q, ans;
std::vector<node>edge[110];
int F[110][30010], cnt[110];

void DFS(int x, int fa)
{
	for (int i = 0; i < edge[x].size(); i++) 
	{
		if(edge[x][i].v == fa)continue;
		DFS(edge[x][i].v, x);
		cnt[x] += cnt[ edge[x][i].v ] + 1;
		for (int j = std::min(q, son[x]); j >= 1; j--) 
			for (int k = 0; k <= std::min(son[edge[x][i].v], j-1); k++) 
				F[x][j] = std::max(F[x][j], F[x][j-k-1] + F[edge[x][i].v][k] + edge[x][i].val);
	}

	return ;
}

int main()
{
	read(n);read(q);
	int x, y, val;
	for (int i = 1; i <= n-1; i++) 
	{
		read(x);read(y);read(val);
		/**
		node now;
		now.v = y;
		now.val = val;
		edge[x].push_back(now);
		*/
		edge[x].push_back((node){y, val});

		edge[y].push_back((node){x, val});
	}

	DFS(1, 0);

	printf("%d", F[1][q]);
	return 0;
}
